package com.example.server.controller;

import com.example.server.dto.CartDto;
import com.example.server.entity.Cart;
import com.example.server.entity.CartItem;
import com.example.server.entity.CartWithCartItem;
import com.example.server.entity.Product;
import com.example.server.redis.RedisService;
import com.example.server.service.CartItemService;
import com.example.server.service.CartService;
import com.example.server.util.Converter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("cart")
@RestController
public class CartController extends BaseController{
    CartService cartService;
    CartItemService cartItemService;
    @Autowired
    RedisService redisService;
    Converter<Product, CartDto> converter;

    @Autowired

    public CartController(CartService cartService, CartItemService cartItemService, Converter<Product, CartDto> converter) {
        this.cartService = cartService;
        this.cartItemService = cartItemService;
        this.converter = converter;
    }

    Map<Integer, List<CartItem>> map = new HashMap<>();
    Map<Integer,CartItem> map2 = new HashMap<>();
//    @GetMapping("query")
//    private List<CartItem> queryAll(){
//        int uid = (int) session.getAttribute("uid");
//    }
    @PostMapping("create")
    private int create(@RequestBody CartItem cartItem) {
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        int pid = Integer.parseInt(redisService.getSetString("productId"));
        System.out.println("================" + cartItem.getNumber());
        Cart cart = new Cart();
        cart.setUid(uid);
        cartItem.setPid(pid);
        List<CartItem> list = cartItemService.queryList("queryByUid", uid);
        map.put(uid, list);
        Map<String,Integer> map3 = new HashMap<>();
        map3.put("uid",uid);
        map3.put("pid",pid);
        CartItem cartItem3 = cartItemService.queryOne("queryItem",map3);
        map2.put(pid,cartItem3);
//        for (CartItem cartItem2:list){
//            System.out.println("123" + cartItem2);
//        }
//        System.out.println(map2.get(pid));
        if (map.get(uid).isEmpty()) {
            cartService.create(cart);
            cartItem.setC_id(cart.getId());
            int i = 0;
            i = cartItemService.create(cartItem);
            System.out.println(i);
            return i;
        } else {
            int sid = cartService.queryNum("querySid", pid);
            /*
             * 判断添加的商品在购车中有没有同店铺下的商品，如有使用同一个购物车id
             * */
            for (CartItem cartItem1 : map.get(uid)) {
                int sid1 = cartService.queryNum("querySid", cartItem1.getPid());
                if (sid == sid1 && map2.get(pid) == null) {
                    int pid1 = cartItem1.getPid();
                    Map<String,Integer> map1 = new HashMap<>();
                    map1.put("pid",pid1);
                    map1.put("uid",uid);
                    int cid = cartItemService.queryNum("queryCid", map1);
                    cartItem.setC_id(cid);
                    return cartItemService.create(cartItem);
                }
                if(cartItem1.getPid() == pid){
                    cartItem.setId(cartItem1.getId());
                    cartItem.setNumber(cartItem.getNumber() + cartItem1.getNumber());
                    return cartItemService.modifyOne("addNum",cartItem);
                }
            }
            cartService.create(cart);
            cartItem.setC_id(cart.getId());
        }
        return cartItemService.create(cartItem);
    }

    @GetMapping("queryCart")
    private List<Cart> queryCart(){
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        List<Cart> carts =  cartService.queryList("queryCart",5);
        return carts;
    }

    @GetMapping("queryProduct")
    private List<Cart> queryByUid(){
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        List<Cart> carts =  cartService.queryList("queryProduct",uid);
        List<Cart> cart2 = new ArrayList<>();
        for (Cart cart : carts){
            List<CartItem> cartItems = cartItemService.queryList("queryCartItem",cart.getId());
            Cart cart1 = new Cart();
            cart1.setId(cart.getId());
            cart1.setName(cart.getStore().getName());
            cart1.setCartItems(cartItems);
            cart2.add(cart1);
        }
        return cart2;
    }

    @PutMapping("modify")
    private void add(@RequestBody CartItem cartItem){
        CartItem cartItem1 = new CartItem();
        cartItem1.setId(cartItem.getId());
        cartItem1.setNumber(cartItem.getNumber());
        cartItemService.modifyOne("addNum",cartItem1);
    }

    @DeleteMapping("removeById/{id}")
    private int removeById(@PathVariable int id){
        return cartItemService.removeById(id);
    }
}

